package com.dancun.controller;


import com.dancun.entity.CTask;
import com.dancun.entity.TGroup;
import com.dancun.entity.TResult;
import com.dancun.service.ICTaskService;
import com.dancun.service.IStudentService;
import com.dancun.service.ITGroupService;
import com.dancun.service.ITResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-10-02
 */
@RestController
public class TGroupController {

    @Autowired
    ITGroupService tGroupService;

    @Autowired
    IStudentService studentService;
    @Autowired
    ITResultService tResultService;
    @Autowired
    ICTaskService cTaskService;

    @RequestMapping("group")
    public ModelAndView list(String tid) {
        ModelAndView mav = new ModelAndView("/Main/Group.jsp");

        List<TGroup> groupList = tGroupService.list();
        String[] str = new String[groupList.size()];
        List<Map> list = new ArrayList<Map>();
        for (int i = 0; i < groupList.size(); i++) {
            if (!groupList.get(i).getTaskId().equals(tid)) {

            } else {
                try {
                    groupList.get(i).setStno1(studentService.getById(groupList.get(i).getStno1()).getSname());
                    groupList.get(i).setStno2(studentService.getById(groupList.get(i).getStno2()).getSname());
                    groupList.get(i).setStno3(studentService.getById(groupList.get(i).getStno3()).getSname());

                    // TODO: handle exception
                    System.out.println("鏈弧鍛�");
                    if (groupList.get(i).getStno3() == null || groupList.get(i).getStno3().equals("null")) {
                        groupList.get(i).setStno3("");
                    }
                    if (groupList.get(i).getStno2() == null || groupList.get(i).getStno2().equals("null")) {
                        groupList.get(i).setStno2("");
                    }
                    if (groupList.get(i).getStno1() == null || groupList.get(i).getStno1().equals("null")) {
                        groupList.get(i).setStno1("");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (groupList.get(i).getStno3() == null || groupList.get(i).getStno3().equals("null")) {
                        groupList.get(i).setStno3("");
                    }
                    if (groupList.get(i).getStno2() == null || groupList.get(i).getStno2().equals("null")) {
                        groupList.get(i).setStno2("");
                    }
                    if (groupList.get(i).getStno1() == null || groupList.get(i).getStno1().equals("null")) {
                        groupList.get(i).setStno1("");
                    }
                }

                TResult t = new TResult();
                t.setTaskId(groupList.get(i).getTaskId());
                t.setGroupId(groupList.get(i).getGroupId());
                TResult tr = tResultService.find(t);
                String attachment= "";
                if (tr != null && tr.getSubmit() != null) {
                    str[i] = (tr.getSubmit().equals("0") ? "未提交" : "已提交");
                    attachment =tr.getAttachment();
                } else {
                    str[i] = "未提交";
                }


                Map map = new HashMap<String, Object>();
                map.put("attachment",attachment);
                map.put("str", str[i]);
                map.put("group", groupList.get(i));
                list.add(map);
            }
        }

        CTask task = cTaskService.getById(tid);
        mav.addObject("cTask", task);
        mav.addObject("list", list);
        mav.addObject("tid", tid);
        return mav;
    }

    @RequestMapping("addGroup")
    public ModelAndView save(TGroup group) {
        ModelAndView mav = new ModelAndView("/listTask");
        if (group.getStno3() == null || group.getStno3().equals("null")) {
            group.setStno3("");
        }
        if (group.getStno2() == null || group.getStno2().equals("null")) {
            group.setStno2("");
        }
        if (group.getStno1() == null || group.getStno1().equals("null")) {
            group.setStno1("");
        }
        group.setTaskId(group.getCTask().getTaskId());
        if (tGroupService.save(group)) {
//            tGroupService.find(group);
            TResult tResult = new TResult();
            tResult.setTaskId(group.getTaskId());
            tResult.setSubmit("0");
            tResult.setAttachment("");
            tResult.setGroupId(group.getGroupId());
            if (tResultService.save(tResult)) {
                return mav;
            } else {
                tResultService.removeById(group.getGroupId());
            }

        }

        mav.setViewName("/listTask");
        return mav;
    }
    @RequestMapping("delGroup")
    @ResponseBody
    public Map del(TGroup group) {
        boolean state = false;
        Map map = new HashMap();
        try{
            tGroupService.removeById(group.getGroupId());
            state=true;
            map.put("message","");
        }catch (Exception e){
            e.printStackTrace();
            map.put("message",e.getStackTrace().toString());
        }
        map.put("state",state);

        map.put("data","");

        return  map;
    }



}
